﻿using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using Catalyst;
using Catalyst.Xml.Serialization;
using SubSonic;

namespace NorthwindBusinessObjects
{
	public class TerritoryCollection : BusinessObjectList<Territory, TerritoryCollection>
	{
		public override TableSchema.Table GetTableSchema()
		{
			return Northwind.DAL.Territory.Schema;
		}
	}

	[XmlSchemaProvider("ProvideSchema")]
	public class Territory : BusinessObject<Territory>, IXmlSerializable
	{
		public struct MappingNames
		{
			public const string Id = "Id";
			public const string Decription = "Decription";
			public const string Region = "Region";
		}

		public override MappingList GetMappings()
		{
			MappingList mappings = new MappingList();
			mappings.Add<int>(new DataMapping(MappingNames.Id, Northwind.DAL.Territory.TerritoryIDColumn));
			mappings.Add<string>(new DataMapping(MappingNames.Decription, Northwind.DAL.Territory.TerritoryDescriptionColumn));
			mappings.Add<Region>(new DataMapping(MappingNames.Region, Northwind.DAL.Territory.RegionIDColumn));

			return mappings;
		}

		public override TableSchema.Table GetTableSchema()
		{
			return Northwind.DAL.Territory.Schema;
		}

		public int Id
		{
			get { return GetObjectValue<int>(MappingNames.Id); }
		}

		public string Description
		{
			get { return GetObjectValue<string>(MappingNames.Decription); }
		}

		public Region Region
		{
			get { return GetObjectValue<Region>(MappingNames.Region); }
		}

		#region IXmlSerializable Members

		public static XmlQualifiedName ProvideSchema(XmlSchemaSet SchemaSet)
		{
			return XmlSerializer<Territory>.ProvideSchema(SchemaSet, Namespaces.Default);
		}

		public XmlSchema GetSchema()
		{
			return null;
		}

		public void ReadXml(XmlReader reader)
		{
			XmlSerializer<Territory>.Deserialize(reader, this);
		}

		public void WriteXml(XmlWriter writer)
		{
			XmlSerializer<Territory>.Serialize(writer, this);
		}

		#endregion
	}
}
